package com.bwie.testlibrary;

import android.arch.lifecycle.AndroidViewModel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.bwie.lib_core.network.HttpRetrofitFactory;
import com.bwie.lib_core.network.HttpType;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class TestNetWorkActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.send_post_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        createLogin();
    }

    //构建网络请求方法
    //登录接口需要使用sign签名防止本地网络请求被篡改
    //sign加密方式固定md5->sign加密内容为username,userpwd,time
    //存在顺序关系name pwd time->顺序固定
    //由于字符串abcd 与 dcba加密值不同导致加密结果不同->所以在生成请求参数时
    //注意先后顺序
    //公司的加密sign 把加密的string字符串做升序操作
    private void createLogin(){
        //生成参数发起网络请求
        //unix时间戳->服务器时间戳精确到s;Android获取的时间戳精确到ms
        //Android获取的时间/1000->保证与服务器时间单位统一
        String requestStr =  "{\"uName\":\"zxy\",\"uPwd\":\"123456\"," +
                "\"time\":\""+(System.currentTimeMillis()/1000)+"\"}";
        Log.e("ZXY","createLogin():"+requestStr);
        //生成本次网络请求body
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),requestStr);
        HttpRetrofitFactory.getInstance().factory(HttpType.SIGNINTERFACE)
                .getRetrofit().create(Api.class).loginUser(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserEntity userEntity) {
                        Log.e("ZXY","onNext:"+userEntity.statuesCode+"&&"+userEntity.msg);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ZXY","onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
